package com.sumbo.config.pipeline;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.sumbo.config.reader.ReaderConfig;
import com.sumbo.config.writer.OutputContext;
import com.sumbo.config.writer.WriterFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * \* User: MeiZhongHao
 * \* Date: 2019-06-10
 * \* Time: 11:16
 * \* Description: 管道配置类
 * \
 */
public class PipelineConfig {

    private static Lock lock = new ReentrantLock();

    private static Map<String, Pipeline> pipelines = new HashMap<>();

    private static Multimap<String, Pipeline> pipelineSearch = ArrayListMultimap.create();

    public static void init() {
        Properties prop = new Properties();
        InputStream in = ReaderConfig.class.getClassLoader().getResourceAsStream(
                "config.properties");
        try {
            prop.load(in);
            initPipelineConfig(prop);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void initPipelineConfig(Properties prop) {
        String names;
        Map<String, Pipeline> temp = new ConcurrentHashMap<>();
        if (prop.containsKey("pipeline.name") && (names = prop.getProperty("pipeline.name")) != null && names.length() > 0) {
            String[] pipelinesNames = names.split(",");
            for (String pipe : pipelinesNames) {
                String pre = "pipeline." + pipe + ".";
                String input = prop.getProperty(pre + "input");
                String processor = prop.getProperty(pre + "processor");
                List<OutputContext> outputs = initOutput(pipe, prop);
                temp.put(pipe, new Pipeline(input, processor, outputs));
            }
        }
        try {
            lock.lock();
            pipelines = temp;
            pipelineSearch = conf2searchMap(temp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private static Multimap<String, Pipeline> conf2searchMap(Map<String, Pipeline> map) {
        Multimap<String, Pipeline> tempSearch = ArrayListMultimap.create();
        if (!map.isEmpty()) {
            for (Pipeline pipeline : map.values()) {
                tempSearch.put(pipeline.getInput(), pipeline);
            }
        }
        return tempSearch;
    }

    private static List<OutputContext> initOutput(String pipeline, Properties prop) {
        String pre = "pipeline." + pipeline + ".";
        String outputs = prop.getProperty(pre + "output");
        String[] outputClients = null;
        if (outputs == null || (outputClients = outputs.split(",")) == null || outputClients.length <= 0) {
            return null;
        }
        List<OutputContext> list = new ArrayList<>();
        for (String client : outputClients) {
            Properties properties = new Properties();
            for (String key : prop.stringPropertyNames()) {
                if (key.startsWith(pre + client + ".")) {
                    properties.setProperty(key.replace(pre + client + ".", ""), prop.getProperty(key));
                }
            }
            list.add(WriterFactory.analysis(client, properties));
        }
        return list;
    }

    public static Collection<Pipeline> getPipelineSearch(String client) {
        return pipelineSearch.get(client);
    }

}